package auction.server;

import java.io.IOException;


import org.apache.log4j.Logger;

public class ShutdownThread  extends Thread{
	public static Logger log = Logger.getLogger(ShutdownThread.class);
	
	public ShutdownThread() {
	}
	
	public void run() {
		while(Server.isResume()){
			String input = "";

			try{
				input = Server.getInReadConsole().readLine();
			} catch (IOException e) {
				log.error("Could not listen for input - "+e.getMessage());
				break;
			} 
			
			//listens for a shutdown event
			if(input.equals("!end")||input.equals("")||input.equals("!exit")){//shutdown		
				Server.endClients();
				Server.setResume(false);	 					
				Server.shutdown();					
    					
				System.out.println("Shutting down Server!");
				break;
			}
			
			//close serversocket
			else if(input.equals("!close")){
				Server.closeServerSocket();
				log.debug("Close ServerSocket");
			}
			else if (input.equals("!reconnect")){ 
				Server.reconnectServerSocket();
				log.debug("Reconnect ServerSocket");
				
			}
					
		}
		log.debug("ShutdownThread stopped");
	}
}
